package com.myfoon.model.cacheable;


import com.myfoon.model.entity.Country;
import com.myfoon.model.entity.User;
import com.myfoon.model.entity.UserPi;

import com.tangosol.io.ExternalizableLite;
import com.tangosol.util.ExternalizableHelper;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import java.sql.Date;
import java.sql.Timestamp;


public class UserCache implements ExternalizableLite, Comparable {
    private long id;
    private String email;
    private String passwd;

    private String photoNormal;
    private String photoSmall;

    private long userPiId;
    private String fullname;
    private Boolean gender;
    private Date birthdate;
    private Integer countryId;

    private short status;

    private Timestamp created;

    public UserCache() {

    }

    public UserCache(User user) {
        this.id = user.getId();
        this.email = user.getEmail();
        this.passwd = user.getPasswd();

        this.photoNormal = user.getPhotoNormal();
        this.photoSmall = user.getPhotoSmall();

        this.userPiId = user.getUserPi().getId();
        this.fullname = user.getUserPi().getFullname();
        this.gender = user.getUserPi().getGender();
        this.birthdate = user.getUserPi().getBirthdate();
        this.countryId = user.getUserPi().getCountry().getId();

        this.status = user.getStatus();
        this.created = user.getCreated();
    }

    public void readExternal(DataInput dataInput) throws IOException {
        id = ExternalizableHelper.readLong(dataInput);
        email = ExternalizableHelper.readSafeUTF(dataInput);
        passwd = ExternalizableHelper.readSafeUTF(dataInput);

        photoNormal = ExternalizableHelper.readSafeUTF(dataInput);
        photoSmall = ExternalizableHelper.readSafeUTF(dataInput);

        userPiId = ExternalizableHelper.readLong(dataInput);
        fullname = ExternalizableHelper.readSafeUTF(dataInput);
        gender = dataInput.readBoolean();
        birthdate = ExternalizableHelper.readDate(dataInput);
        countryId = ExternalizableHelper.readInt(dataInput);

        status = dataInput.readShort();
        created = ExternalizableHelper.readTimestamp(dataInput);
    }

    public void writeExternal(DataOutput dataOutput) throws IOException {
        ExternalizableHelper.writeLong(dataOutput, id);
        ExternalizableHelper.writeSafeUTF(dataOutput, email);
        ExternalizableHelper.writeSafeUTF(dataOutput, passwd);

        ExternalizableHelper.writeSafeUTF(dataOutput, photoNormal);
        ExternalizableHelper.writeSafeUTF(dataOutput, photoSmall);

        ExternalizableHelper.writeLong(dataOutput, userPiId);
        ExternalizableHelper.writeSafeUTF(dataOutput, fullname);
        dataOutput.writeBoolean(gender);
        ExternalizableHelper.writeDate(dataOutput, birthdate);
        ExternalizableHelper.writeInt(dataOutput, countryId);

        dataOutput.writeShort(status);
        ExternalizableHelper.writeTimestamp(dataOutput, created);
    }

    public User toUser() {
        User user = new User();

        user.setId(getId());
        user.setEmail(getEmail());
        user.setPasswd(getPasswd());
        user.setPhotoNormal(getPhotoNormal());
        user.setPhotoSmall(getPhotoSmall());
        user.setStatus(getStatus());
        user.setCreated(getCreated());

        user.setUserPi(new UserPi());
        user.getUserPi().setId(getUserPiId());
        user.getUserPi().setFullname(getFullname());
        user.getUserPi().setGender(getGender());
        user.getUserPi().setBirthdate(getBirthdate());

        user.getUserPi().setCountry(new Country());
        user.getUserPi().getCountry().setId(getCountryId());

        return user;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPhotoNormal(String photoNormal) {
        this.photoNormal = photoNormal;
    }

    public String getPhotoNormal() {
        return photoNormal;
    }

    public void setPhotoSmall(String photoSmall) {
        this.photoSmall = photoSmall;
    }

    public String getPhotoSmall() {
        return photoSmall;
    }

    public void setUserPiId(long userPiId) {
        this.userPiId = userPiId;
    }

    public long getUserPiId() {
        return userPiId;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getFullname() {
        return fullname;
    }

    public void setGender(Boolean gender) {
        this.gender = gender;
    }

    public Boolean getGender() {
        return gender;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public void setCountryId(Integer countryId) {
        this.countryId = countryId;
    }

    public Integer getCountryId() {
        return countryId;
    }

    public void setStatus(short status) {
        this.status = status;
    }

    public short getStatus() {
        return status;
    }

    public void setCreated(Timestamp created) {
        this.created = created;
    }

    public Timestamp getCreated() {
        return created;
    }

    public int compareTo(Object o) {        
        if ( this.id < ((UserCache)o).getId() ) return -1;
        if ( this.id > ((UserCache)o).getId() ) return 1;    
        return 0;
    }
}
